﻿using System;
using System.IO;
namespace SKBFirst
{
    static class Program
    {
        const string Input = "input.txt";
        const string Output = "output.txt";

        static void Main()
        {
            var start = DateTime.Now;
            try
            {
                using (var fileWriter = new StreamWriter(Output))
                using (var fileReader = new StreamReader(Input))
                {
                    fileReader.ReadLine();
                    var initElement = fileReader.ReadLine().Split(new[] {' ', '\t'},
                                                                  StringSplitOptions.RemoveEmptyEntries);
                    var outputAccum = new IntOutput(fileWriter);
                    var treeRoot = TreeNode.Create(outputAccum);
                    var pagination = Pagination.Create(initElement, treeRoot);
                    var count = int.Parse(fileReader.ReadLine());
                    for (int i = 0; i < count; i++)
                        pagination.Process(fileReader.ReadLine());
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
            }
            Console.WriteLine(DateTime.Now - start);
        }
    }
}